package com.itheima.web3.web;

import com.itheima.web3.pojo.User;
import com.itheima.web3.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/v1/register")
public class RegisterServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User user = new User();
        user.setUsername(req.getParameter("username"));
        user.setPassword(req.getParameter("password"));
        System.out.println(user);
        UserServiceImpl userService = new UserServiceImpl();
        boolean f = userService.queryUserByUsername(user);
        if(f) {
            if(userService.addUser(user)>0) resp.getWriter().println(user.getUsername()+"注册成功");
            else resp.getWriter().println("因服务器原因注册失败");

        }else resp.getWriter().println(user.getUsername()+"已注册");
    }
}
